package controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import service.GroupmemberService;

@Controller
public class groupMemberController {
	@Autowired
	@Qualifier("gmsi")
	private GroupmemberService gService;

	@RequestMapping("fileUpLord")
	public String go() {
		return "fileUpLord";
	}

	@RequestMapping(value = "/secondsave", method = RequestMethod.POST)
	public String secondsave(@RequestParam(value = "file1") MultipartFile file,
			HttpServletRequest request) {
		try {
			// 上传后的地址，注意("/upload")是表示文件上传后的目标文件夹
			String realPath = request.getSession().getServletContext()
					.getRealPath("/upload");
			System.out.println("打印文件上传的路径" + realPath);
			// 获取文件名
			String filename = file.getOriginalFilename();
			// 获取文件后缀名
			String extensionname = filename
					.substring(filename.lastIndexOf(".") + 1);
			// 给上传的文件起别名，有很多种方式
			String newFilename = String.valueOf(System.currentTimeMillis())
					+ "." + extensionname;
			// 创建File对象，传入目标路径参数，和新的文件别名
			File dir = new File(realPath, newFilename);
			if (!dir.exists()) {// 如果dir代表的文件不存在，则创建它，
				dir.mkdirs();//
			}
			// 如果存在则直接执行下面操作
			file.transferTo(dir);// 将上传的实体文件复制到指定目录upload下
			System.out.println("测试是否执行完毕");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "fileUpLord";// 执行完毕，返回一个逻辑视图
	}

	public GroupmemberService getgService() {
		return gService;
	}

	public void setgService(GroupmemberService gService) {
		this.gService = gService;
	}
}
